package com.java.design.observer.customer;

import java.util.ArrayList;

/**
 * 天气主题*
 * @author Tony
 */
public class WeatherData implements Subject{

    /**
     * 观察者*
     */
    private ArrayList<ObServer> observers;

    private float temp;

    private float humidity;

    private float pressure;

    public WeatherData(){
        observers = new ArrayList<>(8);
    }

    @Override
    public void registerObserver(ObServer obServer) {
        //添加订阅者
        observers.add(obServer);
    }

    @Override
    public void removeObserver(ObServer obServer) {
        //移除订阅者
        int index = observers.indexOf(obServer);
        if(index >= 0){
            observers.remove(obServer);
        }
    }

    @Override
    public void notifyObservers() {
        for (ObServer obServer : observers) {
            obServer.update(temp, humidity, pressure);
        }
    }

    public void measurementsChanged(){
        notifyObservers();
    }

    public void setMeasurements(float temp,float humidity,float pressure){
        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
}
